package spring.batch.readDatabase;

import org.apache.commons.io.FileUtils;
import org.springframework.batch.item.ItemWriter;
import spring.batch.readFile.People;

import java.io.File;
import java.util.List;

/**
 * @Author: huhx
 * @Date: 2017-11-02 上午 10:19
 */
public class UserFileWriter implements ItemWriter<People> {

    private String targetDirectory;
    private String targetFile;

    public void setTargetDirectory(String targetDirectory) {
        this.targetDirectory = targetDirectory;
    }

    public void setTargetFile(String targetFile) {
        this.targetFile = targetFile;
    }

    @Override
    public void write(List<? extends People> items) throws Exception {
        File targetDirectoryAsFile = new File(targetDirectory);
        if (!targetDirectoryAsFile.exists()) {
            FileUtils.forceMkdir(targetDirectoryAsFile);
        }
        File target = new File(targetDirectory, targetFile);
        FileUtils.writeLines(target, items, true);
    }
}
